/**
 *Copyright(c) HNA SYSTEMS Co., LTD
 *
 *@date 2013-7-24
 * 	
 *Original Author: 郑兴(zhengxing)
 *
 *ChangeLog
 *
 */
package com.hnas.sys.dao;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.Query;

import org.springframework.stereotype.Component;

import com.hnas.core.dao.BaseDaoImpl;
import com.hnas.core.util.StringUtil;
import com.hnas.sys.domain.ResourceStatus;
import com.hnas.sys.orm.Resource;
import com.hnas.sys.service.system.criteria.ResourceCriterial;

/**
 * @author 郑兴(zhengxing)
 *
 */
@Component("resourceDAO")
public class ResourceDAOImpl extends BaseDaoImpl<Resource> implements ResourceDAO {
	public ResourceDAOImpl() {
		super(Resource.class);
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.dt.dao.basic.ResourceDAO#loadAll()
	 */
	@SuppressWarnings("unchecked")
	public List<Resource> loadAll() {
		Query query = entityManager.createQuery("From Resource r where r.statusStr='VALID' order by menuPackageID,priority");
		return query.getResultList();
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.hna.dt.dao.system.ResourceDAO#loadAllReference()
	 */
	@SuppressWarnings("unchecked")
	public List<Resource> loadAllReference() {
		Query query = entityManager.createQuery("From Resource r left join fetch r.authorityResources ar left join fetch ar.authority where r.statusStr='VALID'");
		return query.getResultList();
	}

	/*
	 * (non-Javadoc)
	 * @see com.hna.dt.dao.system.ResourceDAO#queryResource()
	 */
	public List<Resource> queryResource(ResourceCriterial criterial) {
		StringBuffer queryStr = new StringBuffer("From Resource r where 1 = 1 ");
		if(StringUtil.isNotEmpty(criterial.getName())) {
			queryStr.append("and name like ? ");
		}
		if(StringUtil.isNotEmpty(criterial.getResource())) {
			queryStr.append("and resource like ? ");
		}
		if(StringUtil.isNotEmpty(criterial.getStatus())) {
			queryStr.append("and statusStr = ? ");
		}
		Query query = entityManager.createQuery(queryStr.toString());
		//添加参数
		int index = 1;
		if(StringUtil.isNotEmpty(criterial.getName())) {
			query.setParameter(index ++, "%" + criterial.getName() + "%");
		}
		if(StringUtil.isNotEmpty(criterial.getResource())) {
			query.setParameter(index ++, "%" + criterial.getResource() + "%");
		}
		if(StringUtil.isNotEmpty(criterial.getStatus())) {
			query.setParameter(index ++, criterial.getStatus());
		}
		
		return query.getResultList();
	}
	
	public List<Resource> getResources(Long[] authorityIds) {
		// TODO Auto-generated method stub
		if (authorityIds.length==0){
			return new ArrayList<Resource>();
		}
		StringBuffer sb = new StringBuffer("select t.resource from AuthorityResource t where t.resource.statusStr = 'VALID' and t.resource.typeStr ='PAGE'  and t.authority.id in (");
		Object[] objs = new Object[authorityIds.length];
		for (int i=0;i<authorityIds.length;i++){
			sb.append("?").append(i+1).append(",");
			objs[i] = authorityIds[i];
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(") order by t.resource.priority");
		List<Resource > ls= this.findByQuery(sb.toString(), objs);
		return ls;
	}

	public List<Resource> getResources(long menuPackageID, String menuName, int operateType) {
		// TODO Auto-generated method stub
		String hql = "From Resource r where r.statusStr=?1 and r.menuPackageID=?2 and r.name=?3 and r.operateType=?4";
		return this.findByQuery(hql, ResourceStatus.VALID.getAlias(),menuPackageID,  menuName,  operateType);
	}

	public List<Resource> getResourceByIds(long[] ids) {
		// TODO Auto-generated method stub
//		return null;
		if (ids==null || ids.length==0){
			return new ArrayList<Resource>();
		}
		StringBuffer sb = new StringBuffer("select t from Resource t where t.statusStr = 'VALID'  and t.id in (");
		Object[] objs = new Object[ids.length];
		for (int i=0;i<ids.length;i++){
			sb.append("?").append(i+1).append(",");
			objs[i] = ids[i];
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(") ");
		List<Resource > ls= this.findByQuery(sb.toString(), objs);
		return ls;
	}
	public List<Resource> getResourceRefs(long[] ids) {
		// TODO Auto-generated method stub
//		return null;
		if (ids==null || ids.length==0){
			return new ArrayList<Resource>();
		}
		StringBuffer sb = new StringBuffer("select b from Resource a,Resource b  where a.statusStr = 'VALID' and b.statusStr = 'VALID'" +
				" and a.menuPackageID=b.menuPackageID and a.operateType=b.operateType and a.name=b.name and a.id in (");
		Object[] objs = new Object[ids.length];
		for (int i=0;i<ids.length;i++){
			sb.append("?").append(i+1).append(",");
			objs[i] = ids[i];
		}
		sb.deleteCharAt(sb.length()-1);
		sb.append(") ");
		List<Resource > ls= this.findByQuery(sb.toString(), objs);
		return ls;
	}

	public Resource loadResource(String url) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer("select t from Resource t where t.resource=?1");
		List<Resource> ls = this.findByQuery(sb.toString(), url);
		if (ls.size()>0)
			return ls.get(0);
		return null;
	}

	public boolean hasResource(long userId, String resourceUrl) {
		// TODO Auto-generated method stub
		StringBuffer sb = new StringBuffer("select t from Resource t, AuthorityResource ar, Authority au, User u " +
				"where ar.resource=t and ar.authority=au and au in elements(u.authoritys) and t.resource=?1 and u.id=?2");
		List<Resource> ls = this.findByQuery(sb.toString(),resourceUrl, userId);
		if (ls.size()>0)
			return true;
		return false;
	}
}
